﻿using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ToDoListReminder.Data;

namespace UintTest
{
    
    
    /// <summary>
    ///This is a test class for ConfigTest and is intended
    ///to contain all ConfigTest Unit Tests
    ///</summary>
	[TestClass]
	public class ConfigTest
	{
		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get;
			set;
		}

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion


		/// <summary>
		///A test for Config serialization.
		///</summary>
		[TestMethod]
		public void ConfigSerializationTest()
		{
			// Create source object.
			var sourceObject = new Config();
			Assert.IsNotNull(sourceObject, "Failed create a new instance.");

			sourceObject.Projects.Add(new Project(@"c:\work\test1.tdi"));
			sourceObject.Projects.Add(new Project(@"c:\work\test2.tdi"));
			sourceObject.Projects.Add(new Project(@"c:\work\test3.tdi"));

			// Create writer
			var buffer = new StringBuilder();
			var xmlWriter = XmlWriter.Create(buffer);
			Assert.IsNotNull(xmlWriter, "Failed create strung buffered writer");

			// Create serializer.
			var serializer = new DataContractSerializer(typeof(Config));

			//Serialize.
			serializer.WriteObject(xmlWriter, sourceObject);
			xmlWriter.Flush();

			// Create reader
			var stringReader = new StringReader(buffer.ToString());
			var xmlReader = XmlReader.Create(stringReader);

			// Deserialize
			var deserializedObject = (Config)serializer.ReadObject(xmlReader);
			Assert.IsTrue(sourceObject.ToString() == deserializedObject.ToString(), "Failed serialization/re-serialization");
		}

		/// <summary>
		/// A test for save and restore.
		/// </summary>
		[TestMethod]
		public void SaveRestoreTest()
		{
			// Source data members.
			const string fileName0 = @"c:\work\test1.tdi";
			const string fileName1 = @"c:\work\test2.tdi";
			const string fileName2 = @"c:\work\test3.tdi";

			// Create object.
			var sourceObject = new Config();
			Assert.IsNotNull(sourceObject, "Failed create a new instance.");

			sourceObject.Projects.Add(new Project(fileName0));
			sourceObject.Projects.Add(new Project(fileName1));
			sourceObject.Projects.Add(new Project(fileName2));

			// Begin edit mode.
			sourceObject.BeginEdit();

			// Change property.
			sourceObject.Projects[0].FileName = "hhhhh";
			sourceObject.Projects[1].FileName = "hhhhh";
			sourceObject.Projects[2].FileName = "hhhhh";

			sourceObject.Projects.Add(new Project("uytuytuytuy"));

			// Cancel changes.
			sourceObject.CancelEdit();

			// Check.
			Assert.AreEqual(fileName0, sourceObject.Projects[0].FileName, "Failed save/restore");
			Assert.AreEqual(fileName1, sourceObject.Projects[1].FileName, "Failed save/restore");
			Assert.AreEqual(fileName2, sourceObject.Projects[2].FileName, "Failed save/restore");

			var counter = sourceObject.Projects.Count;
			Assert.AreEqual(3, counter, "Failed save/restore");

		}
	}
}
